# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: leon

import os
import datetime

from absl import app, flags


def iterate_date(start_date: datetime.date, end_date: datetime.date, reverse=False):
  cur_date = start_date
  if reverse:
    while cur_date > end_date:
      yield cur_date
      cur_date -= datetime.timedelta(days=1)
  else:
    while cur_date < end_date:
      yield cur_date
      cur_date += datetime.timedelta(days=1)


def iterate_and_check_move2bp(src_root, trading_dates):
  move2bp_dirs = [
    "Futures.Binance.v1-delivery.l1_realtime_move2bp",
    "Futures.Binance.v1.l1_realtime_move2bp",
    "Futures.Huobi.v1.bbo_move2bp",
    "Futures.Huobi.v1-linear-swap.bbo_move2bp",
    "Futures.Huobi.v1-swap.bbo_move2bp",
    "Futures.Okex.v3.realtime_move2bp",
    "Futures.Okex.v3-swap.realtime_move2bp",
    "Spot.Binance.v1.l1_realtime_move2bp",
    "Spot.Gdax.v1.realtime_move2bp",
  ]
  move2bp_good_dates = []
  for trading_date in trading_dates:
    move2bp_good_dates.append(trading_date)
    for move2bp_dir in move2bp_dirs:
      dir_ = os.path.join(src_root, trading_date.strftime('%Y%m%d'), move2bp_dir)
      if not os.path.exists(dir_):
        move2bp_good_dates.remove(trading_date)
        break
  return move2bp_good_dates


def main(argv):
  src_root = flags.FLAGS.src_root
  start_date = flags.FLAGS.start_date
  end_date = flags.FLAGS.end_date

  start_date = datetime.datetime.strptime(start_date, '%Y%m%d').date()
  end_date = datetime.datetime.strptime(end_date, '%Y%m%d').date()
  trading_dates = [td for td in iterate_date(start_date, end_date)]

  good_dates = iterate_and_check_move2bp(src_root, trading_dates)
  good_dates = [trading_date.strftime('%Y%m%d') for trading_date in good_dates]
  print(good_dates)

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('src_root', None, 'Source fastfeed root dir in archive NFS.')
  flags.DEFINE_string('start_date', None, 'Start from YYYYmmdd.')
  flags.DEFINE_string('end_date', None, 'End to YYYYmmdd, not included.')

  app.run(main)
